package com.demo.采用预编译数据库操作对象;

import java.sql.*;

public class 进行sql语句的增删改 {
    public static void main(String[] args) {
        //定义全局变量
            Connection con=null;
            PreparedStatement psta=null;
        try {
        //注册驱动

                Class.forName("com.mysql.cj.jdbc.Driver");

        //获取数据库链接

                con= DriverManager.getConnection("jdbc:mysql://localhost:3306/xclmf","root","552200");

        //获取操作数据库对象

            String sql="insert into users(people,pwd) values(?,?)";//插入操作(增)
            //String sql="update users set people=?,pwd=? where pwd=?";//修改操作(改)
            //String sql="delete from users where pwd=?";//删除操作(删)

            psta=con.prepareStatement(sql);
            psta.setString(1,"jaffc");
            psta.setString(2,"225500");
           // psta.setString(3,"445566");

        //执行sql语句

            int count=psta.executeUpdate();
            if (count != 0) {
                System.out.println("插入成功！");
                //System.out.println("修改成功！");
                //System.out.println("删除成功！");
            }
        //处理结果集
        } catch (SQLException s){
            System.out.println("代码有问题！1");
        } catch (ClassNotFoundException e){
            System.out.println("代码有问题！2");

        }finally {

        //释放资源
            if (psta != null) {
                try {
                    psta.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }

    }
}
